version 19
drop _all

* change the root folder according to your computer directory
global root = "D:\WorkingPaper-Series\Township_Light\Replication_Files"  

global dofiles = "$root\dofile"     
global working_data = "$root\workdata"
global tables = "$root\table"
global figures = "$root\figure"

cd "$working_data"


******************
*****Pre Set******
******************

global geo_ctrl = "topography_t* slope_t* roughness_t*"
global wth_ctrl = "gaez_t* precipitation_t* sunlight_t*"
global dist_ctrl = "county_dist_t* dist_hz_t* dist_coast_t*"

global full_ctrl = "$geo_ctrl $wth_ctrl $dist_ctrl"

global geo_ctrl_1 = "topography_t_1 slope_t_1 roughness_t_1"
global wth_ctrl_1 = "gaez_t_1 precipitation_t_1 sunlight_t_1"
global dist_ctrl_1 = "county_dist_t_1 dist_hz_t_1 dist_coast_t_1"

global full_ctrl_1 = "$geo_ctrl_1 $wth_ctrl_1 $dist_ctrl_1"

global geo_ctrl_2 = "topography_t_2 slope_t_2 roughness_t_2"
global wth_ctrl_2 = "gaez_t_2 precipitation_t_2 sunlight_t_2"
global dist_ctrl_2 = "county_dist_t_2 dist_hz_t_2 dist_coast_t_2"

global full_ctrl_2 = "$geo_ctrl_2 $wth_ctrl_2 $dist_ctrl_2"

global matchvar = "county_dist topography slope gaez sunlight dist_hz dist_coast"


global pre_ctrl = "light_t road_t pop_t"


*********************
***** Figure C4 *****
*********************

*** 1. Donut Design
use "township_panel_final.dta", clear


mat define spill_log = J(9,2,0)

forvalues i = 10 (5) 50 {
	qui reghdfe log_light village treat_trend $full_ctrl if near_dist<5|near_dist>`i', a(county_year idcode) cl(countycode)
	mat spill_log[`=(`i'-5)/5',1] = e(b)[1,1]
	mat spill_log[`=(`i'-5)/5',2] = sqrt(e(V)[1,1])
}

mat define spill_ihs = J(9,2,0)

forvalues i = 10 (5) 50 {
	qui reghdfe ihs_light village treat_trend $full_ctrl if near_dist<5|near_dist>`i', a(county_year idcode) cl(countycode)
	mat spill_ihs[`=(`i'-5)/5',1] = e(b)[1,1]
	mat spill_ihs[`=(`i'-5)/5',2] = sqrt(e(V)[1,1])
}

svmat spill_log 
svmat spill_ihs

keep if spill_log1 != .
keep spill_*

rename _all (coef1 se1 coef2 se2)
gen dist = _n*5+5
reshape long coef se, i(dist) j(outcome)

export delimited using "mat_spillover.csv", replace


*** 2. Direct Estimation
use "township_panel_final.dta", clear

gen near_treat = (year>=near_first_year)

mat define neighbor_log = J(9,2,0)

forvalues i = 10 (5) 50 {
	preserve
	replace near_treat = 0 if near_dist>=`i'
	qui reghdfe log_light near_treat treat_trend $full_ctrl if near_dist!=0, a(county_year idcode) cl(countycode)
	mat neighbor_log[`=(`i'-5)/5',1] = e(b)[1,1]
	mat neighbor_log[`=(`i'-5)/5',2] = sqrt(e(V)[1,1])
	restore
}

mat define neighbor_ihs = J(9,2,0)

forvalues i = 10 (5) 50 {
	preserve
	replace near_treat = 0 if near_dist>=`i'
	qui reghdfe ihs_light near_treat treat_trend $full_ctrl if near_dist!=0, a(county_year idcode) cl(countycode)
	mat neighbor_ihs[`=(`i'-5)/5',1] = e(b)[1,1]
	mat neighbor_ihs[`=(`i'-5)/5',2] = sqrt(e(V)[1,1])
	restore
}

svmat neighbor_log 
svmat neighbor_ihs

keep if neighbor_log1 != .
keep neighbor_*

rename _all (coef1 se1 coef2 se2)
gen dist = _n*5+5
reshape long coef se, i(dist) j(outcome)

export delimited using "mat_neighbor.csv", replace
